def max_m(arr):
    sum_total = sum(arr)
    n = len(arr)
    if sum_total == 0:
        return n  # 特殊情况处理，根据题目可能不需要
    
    # 生成所有可能的m候选（sum_total的因数且不超过数组长度）
    factors = set()
    for i in range(1, int(sum_total**0.5) + 1):
        if sum_total % i == 0:
            if i <= n:
                factors.add(i)
            counterpart = sum_total // i
            if counterpart != i and counterpart <= n:
                factors.add(counterpart)
    # 按从大到小排序候选
    factors = sorted(factors, reverse=True)
    
    # 检查每个候选m
    for m in factors:
        target = sum_total // m
        if can_split(arr, m, target):
            return m
    return 1  # 至少可以分成1份

def can_split(arr, m, target):
    if m == 1:
        return True
    # 检查是否有元素超过目标和
    if any(num > target for num in arr):
        return False
    
    groups = [0] * m  # 记录每个组的当前和
    
    def backtrack(index):
        if index == len(arr):
            return all(g == target for g in groups)
        num = arr[index]
        for i in range(m):
            # 剪枝：避免重复处理相同和的组
            if i > 0 and groups[i] == groups[i-1]:
                continue
            if groups[i] + num > target:
                continue
            # 尝试将当前元素放入第i个组
            groups[i] += num
            if backtrack(index + 1):
                return True
            groups[i] -= num
            # 剪枝：如果当前组放入后又取出且和为0，无需继续尝试其他组
            if groups[i] == 0:
                break
        return False
    
    return backtrack(0)


# 测试用例
if __name__ == "__main__":    
    # 测试用例
    test_cases = [
        [6, 4, 3, 3, 2],
        [10, 8, 6, 4, 2],
        [7, 7, 7],
        [5, 4, 3, 2, 1],
        [5, 4, 3, 2],
    ]
    
    for i, test in enumerate(test_cases):
        print(f"测试用例 {i+1} {test}: 最大的m值为 {max_m(test)}")